
package com.afd.ozblip.photo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.ParcelFileDescriptor;

import java.io.File;
import java.io.FileNotFoundException;

//TODO 修改日志输出
public final class TempFileProvider extends ContentProvider {
    // private static String TAG = "TempFileProvider";

    // private static String AUTHORITY =
    // "cn.cstonline.kartor.content.tempfileprovider";

    public static final int CODE_CAPTURE_IMAGE = 101;
    public static final int CODE_CROP_IMAGE = 102;
    public static final int CODE_SHARE_IMAGE = 103;

    private static final String CONTENT_PATH_CAPTURE_IMAGE = "captureimage";
    private static final String CONTENT_PATH_CROP_IMAGE = "cropimage";

    private static Uri AUTHORITY_URI;

    public static Uri CONTENT_URI_CAPTURE_IMAGE;
    public static Uri CONTENT_URI_CROP_IMAGE;

    private static UriMatcher sURLMatcher;

    public static void init(String authority) {
        AUTHORITY_URI = Uri
                .parse("content://" + authority);

        CONTENT_URI_CAPTURE_IMAGE = Uri.withAppendedPath(
                AUTHORITY_URI, CONTENT_PATH_CAPTURE_IMAGE);
        CONTENT_URI_CROP_IMAGE = Uri.withAppendedPath(
                AUTHORITY_URI, CONTENT_PATH_CROP_IMAGE);

        sURLMatcher = new UriMatcher(
                UriMatcher.NO_MATCH);
        sURLMatcher.addURI(authority, CONTENT_PATH_CAPTURE_IMAGE,
                CODE_CAPTURE_IMAGE);
        sURLMatcher.addURI(authority, CONTENT_PATH_CROP_IMAGE, CODE_CROP_IMAGE);
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode)
            throws FileNotFoundException {
        // if (Log.isLoggable(TAG, Log.VERBOSE)) {
        // Log.d(TAG, "openFile: uri=" + uri + ", mode=" + mode);
        // }
        return getTempFilePfd(sURLMatcher.match(uri));
    }

    private ParcelFileDescriptor getTempFilePfd(int code) {
        final String fileName = getTempFilePath(getContext(), code);
        ParcelFileDescriptor pfd = null;

        try {
            final File file = new File(fileName);

            final File parentFile = file.getParentFile();
            if (!parentFile.exists() && !parentFile.mkdirs()) {
                // Log.e(TAG,
                // "[TempFileProvider] tempStoreFd: "
                // + parentFile.getPath() + "does not exist!");
                return null;
            }

            pfd = ParcelFileDescriptor.open(file,
                    ParcelFileDescriptor.MODE_READ_WRITE
                            | ParcelFileDescriptor.MODE_TRUNCATE
                            | ParcelFileDescriptor.MODE_CREATE);
        } catch (Exception ex) {
            // Log.e(TAG, "getScrapSpacePfd: error creating pfd for " +
            // fileName,
            // ex);
        }

        return pfd;
    }

    public static String getTempFilePath(Context context, int code) {
        switch (code) {
            case CODE_CAPTURE_IMAGE:
                return getInternalTempFilePath(context, "captureimage.jpg");
            case CODE_CROP_IMAGE:
                return getInternalTempFilePath(context, "cropimage.jpg");
            case CODE_SHARE_IMAGE:
                return getExternalTempFilePath(context, "shareimage.jpg");
        }
        return null;
    }

    private static String getInternalTempFilePath(Context context,
            String fileName) {
        // 缓存文件位于内部cache目录下
        return new StringBuilder(context.getCacheDir().getAbsolutePath())
                .append("/").append(fileName).toString();
    }

    private static String getExternalTempFilePath(Context context,
            String fileName) {
        // 缓存文件位于外部cache目录下
        try {
            if (Build.VERSION.SDK_INT > 7) {
                return new StringBuilder(context.getExternalCacheDir()
                        .getAbsolutePath()).append("/").append(fileName)
                        .toString();
            } else {
                return new StringBuilder(Environment
                        .getExternalStorageDirectory().getAbsolutePath())
                        .append("/").append(fileName).toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return "*/*";
    }
}
